package cn.meibang.meibanguserskillstype.provider.service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.apestar.core.base.BaseService;
import cn.apestar.core.base.GenericMapper;
import cn.apestar.dict.api.SysDictAPIService;
import cn.apestar.dict.provider.domain.SysDict;
import cn.apestar.dict.provider.response.SysDictResponse;
import cn.apestar.util.BeanUtilEx;
import cn.meibang.meibanguserskillstype.api.IMeibangUserSkillsTypeAPIService;
import cn.meibang.meibanguserskillstype.provider.dao.MeibangUserSkillsTypeMapper;
import cn.meibang.meibanguserskillstype.provider.domain.MeibangUserSkillsType;
import cn.meibang.meibanguserskillstype.provider.request.ListMeibangUserSkillsTypeRequest;
import cn.meibang.meibanguserskillstype.provider.request.MeibangUserSkillsTypeRequest;
import cn.meibang.meibanguserskillstype.provider.response.MeibangUserSkillsTypeResponse;

/**
 * 技师技能细类型 <br>
 * @author yudw
 * @version 1.0
 */
@Service
public class MeibangUserSkillsTypeService extends BaseService<MeibangUserSkillsType, Long> implements IMeibangUserSkillsTypeAPIService {

    private Logger logger = LoggerFactory.getLogger(MeibangUserSkillsTypeService.class);

    @Autowired
    private MeibangUserSkillsTypeMapper meibangUserSkillsTypeMapper;

    @Autowired
    private SysDictAPIService sysDictApiService;

    @Override
    protected GenericMapper<MeibangUserSkillsType, Long> getMapper() {
        // TODO Auto-generated method stub
        return meibangUserSkillsTypeMapper;
    }

    public void addorUpdate(MeibangUserSkillsTypeRequest record) {
        if (record.getId() == null) {
            if (list(record).size() == 0)
                super.add(record);
        } else
            super.update(record);
    }

    public void del(Long id) {
        MeibangUserSkillsTypeRequest model = new MeibangUserSkillsTypeRequest();
        model.setId(id);
        super.delete(model);
    }

    public List<MeibangUserSkillsTypeResponse> lisForPage(ListMeibangUserSkillsTypeRequest request) {
        return meibangUserSkillsTypeMapper.lisForPage(request);
    }

    public List<MeibangUserSkillsTypeResponse> list(MeibangUserSkillsTypeRequest request) {
        return meibangUserSkillsTypeMapper.list(request);
    }

    public MeibangUserSkillsTypeResponse getByUserId(Long userId) {
        MeibangUserSkillsTypeRequest request = new MeibangUserSkillsTypeRequest();
        request.setUserId(userId);
        request.setDelFlag(true);
        List<MeibangUserSkillsTypeResponse> list = meibangUserSkillsTypeMapper.list(request);
        List<SysDictResponse> mjsList = sysDictApiService.getSysDictResponses("MJS_TYPE");
        List<SysDictResponse> mrsList = sysDictApiService.getSysDictResponses("MRS_TYPE");
        if (list.size() > 0) {
            MeibangUserSkillsTypeResponse returnSkillType = list.get(0);
            convertForDict(mrsList, mjsList, list);
            return returnSkillType;
        } else {
            request.setLevel("0");
            request.setLevel1("1");

            MeibangUserSkillsTypeResponse mei = new MeibangUserSkillsTypeResponse();
            try {
                BeanUtilEx.copyProperties(mei, request);
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mei.setMjsSkillName("美甲师-学员");
            mei.setMrsSkillName("美容美体师-初级");
            addorUpdate(request);
            return mei;
        }
    }

    private void convertForDict(List<SysDictResponse> mrsList, List<SysDictResponse> mjsList, List<MeibangUserSkillsTypeResponse> destList) {
        for (SysDict mrs : mrsList) {
            String code = mrs.getCode();
            for (MeibangUserSkillsTypeResponse skillsTypeResponse : destList) {
                if (Long.parseLong(code) == Long.parseLong(skillsTypeResponse.getLevel1())) {
                    skillsTypeResponse.setMrsSkillName(mrs.getLabel());
                }
            }
        }
        for (SysDict mjs : mjsList) {
            String code = mjs.getCode();
            for (MeibangUserSkillsTypeResponse skillsTypeResponse : destList) {
                if (Long.parseLong(code) == Long.parseLong(skillsTypeResponse.getLevel())) {
                    skillsTypeResponse.setMjsSkillName(mjs.getLabel());
                }
            }
        }
    }

    public void insertList(List<MeibangUserSkillsTypeRequest> list) {
        meibangUserSkillsTypeMapper.insertList(list);
    }
}